<script setup lang="ts">
import { addPatient, editPatient, getPatientList, delPatient } from '@/services/user'
import type { Patient } from '@/types/user'
import { showConfirmDialog, showSuccessToast, showToast } from 'vant'
import { ref, onMounted, computed } from 'vue'
import Validator from 'id-validator'
// 获取患者列表
const list = ref<Patient[]>([])
const getList = async () => {
  const res = await getPatientList()
  list.value = res.data
}
onMounted(() => {
  getList()
})
const options = [
  { label: '男', value: 1 },
  { label: '女', value: 0 }
]
const gender = ref(0)

// 表单初始数据
const initPatient: Patient = {
  name: '',
  idCard: '',
  gender: 1,
  defaultFlag: 0
}
// 侧边弹出层
const show = ref(false)
const showPopup = (item?: Patient) => {
  if (item) {
    // 解构出后台需要的数据
    const { id, gender, name, idCard, defaultFlag } = item
    patient.value = { id, gender, name, idCard, defaultFlag }
  } else {
    patient.value = { ...initPatient }
  }
  show.value = true
}
const patient = ref<Patient>({
  name: '',
  idCard: '',
  gender: 1,
  defaultFlag: 0
})

// 默认值需要转换
// 获取值：如果 defaultFlag 的值是 1，绑定的就是 true，否则是 false
// 设置值：如果 复选框的值是 true，defaultFlag 的值就是 1，否则是 0

const defaultFlag = computed({
  get() {
    return patient.value.defaultFlag === 1 ? true : false
  },
  set(value) {
    patient.value.defaultFlag = value ? 1 : 0
  }
})
// 提交保存
const submit = async () => {
  // 非空校验
  if (!patient.value.name) return showToast('请输入真实姓名')
  if (!patient.value.idCard) return showToast('请输入身份证号')
  // 身份的校验
  const validate = new Validator()
  if (!validate.isValid(patient.value.idCard.trim())) return showToast('身份证格式错误')
  const info = validate.getInfo(patient.value.idCard)
  // 校验性别
  if (info.sex !== patient.value.gender) return showToast('性别和身份证不符')
  // 校验通过 提交保存
  // 判断有没有id 如果有则发起编辑接口
  patient.value.id ? await editPatient(patient.value) : await addPatient(patient.value)
  // 提示用户
  showSuccessToast(patient.value.id ? '编辑成功' : '添加成功')
  // 关闭弹出层
  show.value = false
  // 刷新列表
  getList()
}
const closePopup = () => {
  // 点击返回 让弹出层关闭
  show.value = false
}

// 删除功能

const remove = async () => {
  await showConfirmDialog({
    title: '温馨提示',
    message: `您确认删除${patient.value.name}的患者信息吗?`
  })
  // 调用删除接口
  await delPatient(patient.value.id)
  // 提示用户
  showSuccessToast('删除成功')
  // 关闭弹出层
  show.value = false
  // 刷新列表
  getList()
}
</script>

<template>
  <div class="patient-page">
    <cp-nav-bar title="家庭档案"></cp-nav-bar>
    <div class="patient-list">
      <div class="patient-item" v-for="item in list">
        <div class="info">
          <span class="name">{{ item.name }}</span>
          <span class="id">{{
            item.idCard.replace(/^(.{6})(?:\d)(.{4})$/, '\$1********\$2')
          }}</span>
          <span>{{ item.genderValue }}</span>
          <span>{{ item.age }}岁</span>
        </div>
        <div class="icon"><cp-icon name="user-edit" @click="showPopup(item)" /></div>
        <div class="tag">默认</div>
      </div>

      <div class="patient-add" @click="showPopup()">
        <cp-icon name="user-add" />
        <p>{{ patient.id ? '编辑信息' : '添加患者' }}</p>
      </div>
      <div class="patient-tip">最多可添加 6 人</div>
    </div>
    <!-- 侧边弹出层 -->
    <van-popup v-model:show="show" position="right" :style="{ width: '100%', height: '100%' }">
      <cp-nav-bar
        :title="patient.id ? '编辑信息' : '添加患者'"
        right-text="保存"
        :back="closePopup"
        @click-right="submit"
      ></cp-nav-bar>
      <!-- 表单结构 -->
      <van-form autocomplete="off">
        <van-field label="真实姓名" v-model="patient.name" placeholder="请输入真实姓名" />
        <van-field label="身份证号" v-model="patient.idCard" placeholder="请输入身份证号" />
        <van-field label="性别">
          <!-- 单选按钮组件 -->
          <template #input>
            <cp-radio-btn v-model="patient.gender" :options="options"></cp-radio-btn>
          </template>
        </van-field>
        <van-field label="默认就诊人">
          <template #input>
            <van-checkbox round v-model="defaultFlag" />
          </template>
        </van-field>
      </van-form>
      <!-- 删除 -->
      <van-action-bar v-if="patient.id">
        <van-action-bar-button @click="remove">删除</van-action-bar-button>
      </van-action-bar>
    </van-popup>
  </div>
  <!-- 
  <cp-radio-btn
    :options="options"
    :modelValue="gender"
    @update:moduleValue="gender = $event as number"
  ></cp-radio-btn> -->
  <!-- <cp-radio-btn :options="options" v-model="gender"></cp-radio-btn> -->
</template>

<style lang="scss" scoped>
.patient-page {
  padding: 46px 0 80px;
  ::v-deep() {
    .van-popup {
      width: 100%;
      height: 100%;
      padding-top: 46px;
      box-sizing: border-box;
    }
  }
}
.patient-list {
  padding: 15px;
}
.patient-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: var(--cp-bg);
  border-radius: 8px;
  margin-bottom: 15px;
  position: relative;
  border: 1px solid var(--cp-bg);
  transition: all 0.3s;
  overflow: hidden;
  .info {
    display: flex;
    flex-wrap: wrap;
    flex: 1;
    span {
      color: var(--cp-tip);
      margin-right: 20px;
      line-height: 30px;
      &.name {
        font-size: 16px;
        color: var(--cp-text1);
        width: 80px;
        margin-right: 0;
      }
      &.id {
        color: var(--cp-text2);
        width: 180px;
      }
    }
  }
  .icon {
    color: var(--cp-tag);
    width: 20px;
    text-align: center;
  }
  .tag {
    position: absolute;
    right: 60px;
    top: 21px;
    width: 30px;
    height: 16px;
    font-size: 10px;
    color: #fff;
    background-color: var(--cp-primary);
    border-radius: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  &.selected {
    border-color: var(--cp-primary);
    background-color: var(--cp-plain);
    .icon {
      color: var(--cp-primary);
    }
  }
}
.patient-add {
  background-color: var(--cp-bg);
  color: var(--cp-primary);
  text-align: center;
  padding: 15px 0;
  border-radius: 8px;
  .cp-icon {
    font-size: 24px;
  }
}
.patient-tip {
  color: var(--cp-tag);
  padding: 12px 0;
}

// 底部操作栏
.van-action-bar {
  padding: 0 10px;
  margin-bottom: 10px;
  .van-button {
    color: var(--cp-price);
    background-color: var(--cp-bg);
  }
}
</style>
